﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.Sql;
using System.Data.SqlClient;

namespace pcsse
{
    public class db_base
    {

           static db_base dbentity;
           SqlConnection dbcon;
         
            public static db_base getDBhandle()
            {
                if (dbentity == null)
                    dbentity = new db_base();
                return dbentity;

            }

	        protected db_base()
	        {
                dbcon = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["PCSSE"].ToString());
                dbcon.Open();
                
               
	        }

             ~db_base()
            {
                dbcon.Close();
                dbcon = null;
            }

            public DataTable GetDataTableBySQL(string sql)
            {
                SqlCommand cmd = new SqlCommand(sql, dbcon);
                SqlDataReader dr =  cmd.ExecuteReader();
                DataTable dt = new DataTable();
                dt.Load(dr);
                dr.Close();
                return dt;
            }

            public DataTable GetDataTableByCmd(SqlCommand cmd)
            {
                cmd.Connection = dbcon;
                SqlDataReader dr = cmd.ExecuteReader();
                DataTable dt = new DataTable();
                dt.Load(dr);
                dr.Close();
                return dt;
            }


            public int ExecuteCmd(SqlCommand cmd)
            {
                cmd.Connection = dbcon;
                
                return cmd.ExecuteNonQuery();
            }

            public int ExecuteSql(string sql)
            {
                SqlCommand cmd = new SqlCommand(sql, dbcon);

                return cmd.ExecuteNonQuery();
            }

    }
}
